# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 10:54:54 2019

@author: Administrator
"""

# 1025 反转链表
# 解题思路：
# 分析数据结构，创建一个节点Node类保存相关数据
# 连接节点：输入数据的节点无序，首先根据节点的下一个节点地址，链接节点
# 节点反转：在连接好的节点上，逆序反转节点（注意如果违反转节点不够，则不反转）
# 节点反转后，再重新修正节点的链接关系（即next_addr地址）
# 打印
# 运行超时
"""
class Node():
    
    def __init__(self, address, data, next_addr):
        self.address = address
        self.data = data
        self.next_addr = next_addr
        

def link_node(data, first_addr):
    linked_list = []
    num = len(data)
    # 连接各节点
    address = first_addr
    while len(linked_list) != num:
        for i in range(num):
            if data[i].address == address:
                linked_list.append(data[i])
                address = data[i].next_addr
                break
            
    return linked_list

def reverse(linked_list, step):
    reversed_list = []
    total_len = len(linked_list)
    while len(reversed_list) != total_len:
        if step <= len(linked_list):
            for i in range(step-1,-1,-1):
                reversed_list.append(linked_list.pop(i))
        else:
            for node in linked_list:
                reversed_list.append(node)
    
    # 修正节点next_addr地址
    for j in range(total_len):
        if j != total_len - 1:
            reversed_list[j].next_addr = reversed_list[j+1].address
        else:
            reversed_list[j].next_addr = str(-1)
    
    return reversed_list

def print_reversed_list(reversed_list):
    for node in reversed_list:
        print("%s %d %s" % (node.address, node.data, node.next_addr))

def test():
    raw = str(input()).split(' ')
    
    # 地址：str类型
    first_addr = raw[0]
    num = int(raw[1])
    if num <= 0 or num > 10 ** 5:
        return
    
    step = int(raw[2])
    if step <= 0 or step > num:
        return
    
    data = []
    for i in range(num):
        temp = input().split(' ')
        node = Node(temp[0], float(temp[1]), temp[2])
        data.append(node)
    
    # 连接
    linked_list = link_node(data, first_addr)
    
    # 反转
    reversed_list = reverse(linked_list, step)
    
    # 打印
    print_reversed_list(reversed_list)
    
test()
#data = []
#base = 1000
#for i in range(6):
#    addr = str(base).zfill(5)
#    next_addr = str(base+1).zfill(5)
#    node = Node(addr,i,next_addr)
#    data.append(node)
#    base = base + 1
#    
#print(data)
#reversed_list = reverse(data, 3)
#print_reversed_list(reversed_list)
"""